/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundless.managedbeans;

import boundless.ejb.RequestBean;
import boundless.entities.Account;
import boundless.entities.League;
import boundless.entities.Player;
import java.util.Collection;
import javax.ejb.EJB;
import javax.inject.Inject;

/**
 *
 * @author hendrikdekock
 */
public class LeagueCreationBean {

    
    @EJB
    RequestBean dbAccess;
    
    @Inject
    Account curUser;
    
    String error;
    
    /**
     * Creates a new instance of LeagueCreationbean
     */
    public LeagueCreationBean() {
    }
    
    public void setReqBean(RequestBean rb) {
        dbAccess = rb;
    }
    
    public void setCurUser(Account a) {
        curUser = a;
    }
    
    /**
     * Sends info to the DB, plus the current user to be the owner.
     * Exits on a return of true (successful League Creation), otherwise returns error.
     * @param name : The name for the new League
     * @param inviteOnly : If the new League is invite-only
     */
    public boolean createLeague(String name, boolean inviteOnly) {
        boolean add = dbAccess.createLeague(curUser, name, inviteOnly);
        if(!add)
            return add;
        dbAccess.synchronize();
        League l = dbAccess.getLeagueByName(name);
        Collection<League> coll = curUser.getLeagueCollection();
        coll.add(l);
        Collection<Account> acccoll = l.getAccountCollection();
        acccoll.add(curUser);
        return true;
    }
    
}
